package com.hzl.lease.web.app.service.impl;

import com.hzl.lease.common.context.LoginUserContext;
import com.hzl.lease.model.entity.*;
import com.hzl.lease.model.enums.ItemType;
import com.hzl.lease.model.enums.LeaseStatus;
import com.hzl.lease.web.app.mapper.*;
import com.hzl.lease.web.app.service.ApartmentInfoService;
import com.hzl.lease.web.app.service.BrowsingHistoryService;
import com.hzl.lease.web.app.service.GraphInfoService;
import com.hzl.lease.web.app.service.RoomInfoService;
import com.hzl.lease.web.app.vo.apartment.ApartmentItemVo;
import com.hzl.lease.web.app.vo.attr.AttrValueVo;
import com.hzl.lease.web.app.vo.fee.FeeValueVo;
import com.hzl.lease.web.app.vo.graph.GraphVo;
import com.hzl.lease.web.app.vo.room.RoomDetailVo;
import com.hzl.lease.web.app.vo.room.RoomItemVo;
import com.hzl.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private ApartmentInfoService  apartmentInfoService;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private  AttrValueMapper attrValueMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private  LabelInfoMapper labelInfoMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;

    @Autowired
    private BrowsingHistoryService browsingHistoryService;



    @Override
    public IPage<RoomItemVo> pageItem(Page<Object> objectPage, RoomQueryVo queryVo) {
        return roomInfoMapper.pageItem(objectPage,queryVo);
    }

    @Override
    public RoomDetailVo getDetailById(Long id) {
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        /*
        * getById 方法用于根据主键 ID 查询数据库中的记录,this 代表调用 getDetailById 方法的对象
        * （比如上面例子中的 roomService），而 roomDetailVo 是在方法内部新创建的一个
        * 用于存储房间详情信息的对象，二者代表不同的对象实例
        * */
        //正在调用getById方法这个当前房间对象，它从前端传来id，根据id可以获取到当前房间具体是那个详细的房间，
        // ----所以用roomInfo这个房间对象接收传来的这个房间。
        RoomInfo roomInfo = this.getById(id);//获取前端传来的这个具体房间对象

        // "所属公寓信息"
         ApartmentItemVo apartmentItemVo = apartmentInfoService.selectApartmentInfoByApartmentId(roomInfo.getApartmentId());;

        // "图片列表"
         List<GraphInfo> graphInfoList = graphInfoMapper.selectGraphByItemIdAndItemType(id,ItemType.ROOM);

        //"属性信息列表"
         List<AttrValueVo> attrValueVoList = attrValueMapper.selectAttrValueByRoomId(id);

        // "配套信息列表 -- 公寓"
         List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectFacilityByRoomId(id);

        //"标签信息列表    - 房间"
         List<LabelInfo> labelInfoList = labelInfoMapper.selectLabelByRoomId(id);

        //"支付方式列表"
         List<PaymentType> paymentTypeList = paymentTypeMapper.selectPaymentTypeByRoomId(id);

        //"杂费列表  --公寓"
         List<FeeValueVo> feeValueVoList = feeValueMapper.selectFeeValueByApartmentId(roomInfo.getApartmentId());

        //"租期列表"
        List<LeaseTerm> leaseTermList = leaseTermMapper.selectLeaseTermByRoomId(id);

        // "房间是否被删除"
         Boolean isDelete = roomInfo.getIsDeleted()==1;

        // "房间是否已入住"
        Long count = leaseAgreementMapper.selectCount(new LambdaQueryWrapper<LeaseAgreement>()
                .eq(LeaseAgreement::getRoomId, id)
                .in(LeaseAgreement::getStatus, 2, 5));
        Boolean isCheckIn=count>0;


       /*
       * 将房间的基本信息从 roomInfo 复制到 roomDetailVo 中，并将其他与房间相关的详细信息手动设置到roomDetailVo中，
       * 最终 roomDetailVo 对象将包含完整的房间详细信息，可用于前端展示或其他业务处理。
       * */
         BeanUtils.copyProperties(roomInfo,roomDetailVo);

         roomDetailVo.setApartmentItemVo(apartmentItemVo);
         roomDetailVo.setGraphVoList(graphInfoList);
         roomDetailVo.setAttrValueVoList(attrValueVoList);
         roomDetailVo.setFacilityInfoList(facilityInfoList);
         roomDetailVo.setLabelInfoList(labelInfoList);
         roomDetailVo.setPaymentTypeList(paymentTypeList);
         roomDetailVo.setFeeValueVoList(feeValueVoList);
         roomDetailVo.setLeaseTermList(leaseTermList);
         roomDetailVo.setIsDelete(isDelete);
         roomDetailVo.setIsCheckIn(isCheckIn);

        //记录当前登录用户的浏览历史
        browsingHistoryService.saveHistory(LoginUserContext.get().getUserId(),id);

        return roomDetailVo;
    }

    @Override
    public IPage<RoomItemVo> pageItemByApartmentId(Page<Object> objectPage, Long id) {
        return roomInfoMapper.pageItemByApartmentId(objectPage,id);
    }
}




